package com.wei.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wei.blog.common.dto.BlogPage;
import com.wei.blog.common.status.BlogStatus;
import com.wei.dao.InvitationDao;
import com.wei.entity.InvitationDataEntity;
import com.wei.entity.InvitationEntity;
import com.wei.service.CommentService;
import com.wei.service.ILikeService;
import com.wei.service.InvitationService;
import com.wei.vo.IndexDataVo;
import com.wei.vo.LikeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InvitationServiceImpl extends ServiceImpl<InvitationDao, InvitationEntity> implements InvitationService {

    @Autowired
    private ILikeService likeService;

    @Autowired
    private CommentService commentService;

    @Override
    public Page<IndexDataVo> indexPage(BlogPage<IndexDataVo> blogPage) {
        Page<IndexDataVo> page = baseMapper.selectIndexData(blogPage);
        for(IndexDataVo indexDataVo:page.getRecords()){
            LikeVo likeVo = new LikeVo();
            likeVo.setEntityType(1);

            likeVo.setEntityId(indexDataVo.getInvitationEntity().getId());

            if(indexDataVo.getInvitationDataEntity()==null){
                indexDataVo.setInvitationDataEntity(new InvitationDataEntity());
            }

            //查看帖子的点赞数量
            Long likeCount = likeService.likeCount(likeVo);
            indexDataVo.getInvitationDataEntity().setLikes(
                    Integer.parseInt(String.valueOf(likeCount)
                    )
            );

            //查看帖子的评论数量
            Long commentCount = commentService.getCommentCountByTopicId(
                    BlogStatus.TOPIC_COMMENT.getCode(),
                    indexDataVo.getInvitationEntity().getId()
            );

            indexDataVo.getInvitationDataEntity().setComments(
                    Integer.parseInt(commentCount.toString())
            );

        }
        return page;
    }
}
